package com.java.controller;

import com.java.bean.BootstrapTableUser;
import com.java.bean.Message;
import com.java.bean.ResultData;
import com.java.bean.User;
import com.java.exception.DuplicateException;
import com.java.exception.ExceptionStateEnum;
import com.java.mvc.Body;
import com.java.service.UserService;
import com.java.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class UserController {

    // @Body("/admin/views/user/.do")
    // public String (HttpServletRequest req, HttpServletResponse resp) {

    @Body("/admin/views/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            final int id = Integer.parseInt(req.getParameter("id"));
            if (UserService.delete(id)) {
                return JSONUtil.toJSON(new Message(0, "删除成功"));
            }
        } catch (NumberFormatException ignored) {}
        return JSONUtil.toJSON(new Message(-1, "删除失败"));
    }

    @Body("/admin/views/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            final int id = Integer.parseInt(req.getParameter("id"));
            final String name = req.getParameter("name");
            final String phone = req.getParameter("phone");
            final String email = req.getParameter("email");
            final String passwd = req.getParameter("passwd");
            final User user = new User(name, phone, passwd, null, email);
            final int status = UserService.update(id, user);
            return JSONUtil.toJSON(new Message(status, status != -1 ? "新增用户成功" : "新增用户失败"));
        } catch (NumberFormatException e) {
            return JSONUtil.toJSON(new Message(-1, "更新用户失败"));
        } catch (DuplicateException e) {
            return new Message(e.getType().getVal(), getMessage(e.getType())).toJSON();
        }
    }

    private String getMessage(ExceptionStateEnum e) {
        switch (e) {
            case DUPLICATE_NAME:
                return "昵称已被占用";
            case DUPLICATE_PHONE:
                return "电话号码已被注册";
            case DUPLICATE_EMAIL:
                return "电子邮箱地址已被注册";
            default:
                return "出错了，请重试";
        }
    }

    @Body("/admin/views/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp) {
        final String phone = req.getParameter("phone");
        final User user = UserService.getUserByPhone(phone);
        if (user != null) {
            return JSONUtil.toJSON(new Message(0, "查询成功", new BootstrapTableUser(user)));
        }
        return JSONUtil.toJSON(new Message(-1, "该手机号尚未注册"));
    }

    @Body("/admin/views/user/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        final String name = req.getParameter("name");
        final String phone = req.getParameter("phone");
        final String email = req.getParameter("email");
        final String passwd = req.getParameter("passwd");
        final User user = new User(name, phone, passwd, new Timestamp(new Date().getTime()), email);
        final int status;
        try {
            status = UserService.insert(user);
        } catch (DuplicateException e) {
            return new Message(e.getType().getVal(), getMessage(e.getType())).toJSON();
        }
        return JSONUtil.toJSON(new Message(status, status != -1 ? "新增用户成功" : "新增用户失败"));
    }

    @Body("/admin/views/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        final int offset = Integer.parseInt(req.getParameter("offset"));
        final int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        int total = UserService.total();
        final List<User> users;
        if (total > offset)
            users = UserService.listUser(offset, pageNumber);
        else
            users = UserService.listUser(Math.max(total - pageNumber, 0), pageNumber);
        if (users != null) {
            final ResultData<BootstrapTableUser> data = new ResultData<>();
            for (User user : users) {
                data.add(new BootstrapTableUser(user));
            }
            data.setTotal(total);
            return JSONUtil.toJSON(data);
        }
        return null;
    }
}
